/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Neil C Smith.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 3 for more details.
 *
 * You should have received a copy of the GNU General Public License version 3
 * along with this work; if not, see http://www.gnu.org/licenses/
 *
 *
 * Please visit http://neilcsmith.net if you need additional information or
 * have any questions.
 */
package net.neilcsmith.praxis.live.core.ui;

import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import net.neilcsmith.praxis.live.core.DefaultHubManager;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.ListView;
import org.openide.util.ImageUtilities;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//net.neilcsmith.praxis.live.hubui//HubUI//EN",
autostore = false)
public final class HubUITopComponent extends TopComponent implements ExplorerManager.Provider {

    private static HubUITopComponent instance;
    /** path to the icon used by the component and its open action */
    static final String ICON_PATH = "net/neilcsmith/praxis/live/core/ui/resources/hub-action.png";
    private static final String PREFERRED_ID = "HubUITopComponent";
    private HubProxy hub;
    private ExplorerManager manager;

    public HubUITopComponent() {
        manager = new ExplorerManager();
        hub = new HubProxy();
        manager.setRootContext(hub.getNodeDelegate());
        initComponents();
        ((ListView) rootList).setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setName(NbBundle.getMessage(HubUITopComponent.class, "CTL_HubUITopComponent"));
        setToolTipText(NbBundle.getMessage(HubUITopComponent.class, "HINT_HubUITopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rootList = new ListView();
        jToolBar1 = new javax.swing.JToolBar();
        restartButton = new javax.swing.JButton();
        systemRootToggle = new javax.swing.JToggleButton();

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        restartButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/net/neilcsmith/praxis/live/core/ui/resources/restart.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(restartButton, org.openide.util.NbBundle.getMessage(HubUITopComponent.class, "HubUITopComponent.restartButton.text")); // NOI18N
        restartButton.setToolTipText(org.openide.util.NbBundle.getMessage(HubUITopComponent.class, "LBL_RestartHub")); // NOI18N
        restartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restartButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(restartButton);

        systemRootToggle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/net/neilcsmith/praxis/live/core/ui/resources/system.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(systemRootToggle, org.openide.util.NbBundle.getMessage(HubUITopComponent.class, "HubUITopComponent.systemRootToggle.text")); // NOI18N
        systemRootToggle.setToolTipText(org.openide.util.NbBundle.getMessage(HubUITopComponent.class, "LBL_ShowSystemRoots")); // NOI18N
        systemRootToggle.setFocusable(false);
        systemRootToggle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        systemRootToggle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        systemRootToggle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                systemRootToggleActionPerformed(evt);
            }
        });
        jToolBar1.add(systemRootToggle);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(rootList, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(rootList, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void restartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restartButtonActionPerformed

        DefaultHubManager.getInstance().restart();
    }//GEN-LAST:event_restartButtonActionPerformed

    private void systemRootToggleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_systemRootToggleActionPerformed
        hub.setShowSystemRoots(systemRootToggle.isSelected());
    }//GEN-LAST:event_systemRootToggleActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton restartButton;
    private javax.swing.JScrollPane rootList;
    private javax.swing.JToggleButton systemRootToggle;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized HubUITopComponent getDefault() {
        if (instance == null) {
            instance = new HubUITopComponent();
        }
        return instance;
    }

    /**
     * Obtain the HubUITopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized HubUITopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(HubUITopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof HubUITopComponent) {
            return (HubUITopComponent) win;
        }
        Logger.getLogger(HubUITopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return manager;
    }
}
